package com.naiterui.common.jinshuju.callback.handler;

import java.util.Map;

import org.springframework.stereotype.Component;

import com.naiterui.common.jinshuju.bean.JinshujuCallback;
import com.naiterui.common.jinshuju.bean.JinshujuInfo;
import com.naiterui.common.jinshuju.constants.JinshujuConstants;
import com.naiterui.common.jinshuju.exception.JinshujuException;
import com.naiterui.common.jinshuju.service.JinshujuService;

/**
 * 金数据-限制服务号粉丝填处理策略
 *
 * @author gaoliangliang
 * @date 2019/10/19 2:36 下午
 * @since 1.0.0
 */
@Component
public class WxmpCallbackHandler extends AbstractCallbackHandler {

    @Override
    public JinshujuInfo handle(JinshujuCallback jinshujuCallback, Map<String, Object> context,
                               JinshujuService jinshujuService) throws JinshujuException {

        // 自定义扩展属性
        String openId = jinshujuCallback.getEntry().getOrDefault(JinshujuConstants.JINSHUJU_WX_OPENID_KEY, "").toString();
        String bizId = jinshujuCallback.getEntry().getOrDefault(JinshujuConstants.JINSHUJU_EXTRA_KEY, "").toString();
        String formId = jinshujuCallback.getForm();

        /*if (StringUtils.isAnyEmpty(openId, formId)) {
            throw new JinshujuException(JinshujuExceptionCodes.CALLBACK_NOT_COMPLETE);
        }
*/
        jinshujuCallback.setBizId(bizId);
        jinshujuCallback.setOpenId(openId);
        jinshujuCallback.setFormId(formId);

        return jinshujuService.convertToInfo(jinshujuCallback);

    }
}
